# 导入必要的库
import numpy as np
from lazypredict.Supervised import LazyClassifier
import util
from skimage.feature import hog
from sklearn.metrics import accuracy_score
from util import save_file, get_config_value, load_file
import xgboost

# 1. 加载训练集和测试集
X_train, X_test, y_train, y_test = util.load_file("X_train, X_test, y_train, y_test", f'{get_config_value("Xy_root")}/Xy')

# 2. 使用HOG特征提取
print("使用HOG特征提取...")
hog_features_train = []
hog_features_test = []

for img in X_train:
    hog_features = hog(img.reshape((100, 100)), orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
    hog_features_train.append(hog_features)

for img in X_test:
    hog_features = hog(img.reshape((100, 100)), orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
    hog_features_test.append(hog_features)

X_train_hog = np.array(hog_features_train)
X_test_hog = np.array(hog_features_test)

# 3. 使用LazyClassifier进行模型指标对比
print("开始评估所有的模型:")
clf = LazyClassifier()

# 获取每一个模型的预测对比结果
scores, _ = clf.fit(X_train_hog, X_test_hog, y_train, y_test)
print(scores)

# 4. 获取F1分数最高的模型
# 获取F1分数最高行的索引值，即：模型名称
best_model_name = scores['F1 Score'].idxmax()
print("F1分数最高的模型是: ", best_model_name)
# 根据模型名称，从模型字典中获取模型对象
best_model = clf.models[best_model_name]

# 5. 序列化最佳模型
util.save_file(best_model, "最好的F1分数的模型", f'{get_config_value("model_root")}/best_model')
